﻿using mak_bll;
using Mak_model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;

using System.Windows.Forms;

namespace markets
{
    public partial class buygoods : Form
    {
        /// <summary>
        /// 收银台
        /// </summary>
        public buygoods()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 键盘按下触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">按键</param>
        private void Form1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.F2)
            {
                textBox1.Focus();
            }
            if (e.KeyCode == Keys.F5)
            {
                jiesuan pay = new jiesuan();
                pay.buylist = thisshow;
                pay.ShowDialog();
                bangding();
            }
        }

        /// <summary>
        /// 定义一个data数组用于显示数据
        /// </summary>
        List<data> thisshow = new List<data>();
        /// <summary>
        /// 引用逻辑层
        /// </summary>
        luoji buyallallgoods = new luoji();

        /// <summary>
        /// 定义一个data类用于储存数据
        /// </summary>
       

        /// <summary>
        /// 当收索键按下时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e">按键</param>
        public void button1_Click(object sender, EventArgs e)
        {
            //当搜索框的值不为空
            if (textBox1.Text != "")
            {
                //定义一个数组获取到查询出来的数据（引用DBHelp)
                List<GoodsInfo> lie = buyallallgoods.QueryListByCode(textBox1.Text);
                data item = new data();
                //如果查询出0个
                if (lie.Count == 0)
                {
                    MessageBox.Show("商品编码输入错误");
                }
                //如果查询出1个
                else if (lie.Count == 1)
                {
                    
                 
                       
                        MessageBox.Show(lie[0].GoodsName.ToString());
                        item.GoodsCode = lie[0].GoodsCode.ToString();
                        item.GoodsName = lie[0].GoodsName;
                        item.SalePrice = lie[0].SalePrice;
                        item.goodsCount = 1;
                        lie.Add(item);
                    
                }
                else 
                {
                    //如果查询出多个
                    ShowData xuanze = new ShowData(null);
                    xuanze.show = lie;
                    xuanze.ShowDialog();
                    GoodsInfo sp = xuanze.xuanzeo;
                    if (sp != null)
                    {

                    item.GoodsCode = sp.GoodsCode;
                    item.GoodsName = sp.GoodsName;
                    item.SalePrice = sp.SalePrice;
                    item.goodsCount = 1;
                    thisshow.Add(item);
                        }
                }
                bangding();
                textBox1.Text = "";
                geshu += item.goodsCount;
                zongjia += item.xiaoji;
                label1.Text = zongjia.ToString();
                label4.Text = geshu.ToString();
                
            }
            
        }

        /// <summary>
        /// 刷新
        /// </summary>
        ///
        double zongjia=0.0;
        int geshu = 0;
        public double bangding()
        {
            dataGridView1.DataSource = new BindingList<data>(thisshow);
          
           
                return zongjia;
        }


        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (textBox1.Text != "")
                {
                    button1_Click(null, null);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            jiesuan pay = new jiesuan();
            pay.buylist = thisshow;
            pay.ShowDialog();
            bangding();

        }

        private void buygoods_ui_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
        }

        private void buygoods_FormClosing(object sender, FormClosingEventArgs e)
        {
            
        }

        
    }
}
